package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.exartisan.adapter.GalleryAdapter;
import com.exartisan.bean.GalleryContent;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.NewParamBuilder;
import com.exartisan.network.NewResponseTranslater;
import com.exartisan.network.NewRestConnector;
import com.exartisan.util.CommentDialog;
import com.exartisan.util.DataHolder;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class GalleryActivity extends Activity {

	private ListView listView;
	private GalleryAdapter mAdapter;
	 private ArrayList<GalleryContent> alContent;
	 ArrayList<GalleryContent> photoGallery;
	 ArrayList<GalleryContent> videoGallery;
     private ProgressBar prBar;
     private Button btnRetry;
	
	private Button btnPhoto;
	private Button btnVideo;
	private static boolean isPhotoSegment = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);

		btnPhoto = (Button) findViewById(R.id.btnPhoto);
		btnVideo = (Button) findViewById(R.id.btnVideo);
		listView = (ListView) findViewById(R.id.lvEventContent);
		btnRetry = (Button) findViewById(R.id.btnRetryGallery);
		prBar = (ProgressBar) findViewById(R.id.pro_gallery);

		// arraylist get from response
		/*ArrayList<GalleryContent> afterParsing = new ArrayList<GalleryContent>();
		for (int i = 0; i < 20; i++) {
			GalleryContent content = new GalleryContent(1,
					"http://genk2.vcmedia.vn/DlBlzccccccccccccE5CT3hqq3xN9o/Image/2013/05/PAV/bt4-c4d67.jpg",
					"abc", "def", 1);
			afterParsing.add(content);
		}*/
		// filter

		// default set photo gallery first
		isPhotoSegment = true;
		
		btnRetry.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getData();
			}
		});

		btnPhoto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				isPhotoSegment = true;
				btnPhoto.setBackgroundResource(R.drawable.button_focus_bg);
				btnPhoto.setTextColor(Color.parseColor("#ffffff"));
				btnVideo.setBackgroundResource(R.drawable.button_nonfocus_bg);
				btnVideo.setTextColor(Color.parseColor("#2388c5"));
				mAdapter = new GalleryAdapter(getApplicationContext(),
						photoGallery, isPhotoSegment);
				listView.setAdapter(mAdapter);
			}
		});

		btnVideo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				isPhotoSegment = false;
				btnVideo.setBackgroundResource(R.drawable.button_focus_bg);
				btnVideo.setTextColor(Color.parseColor("#ffffff"));
				btnPhoto.setBackgroundResource(R.drawable.button_nonfocus_2);
				btnPhoto.setTextColor(Color.parseColor("#2388c5"));
				mAdapter = new GalleryAdapter(getApplicationContext(),
						videoGallery, isPhotoSegment);
				listView.setAdapter(mAdapter);
			}
		});
		
		/*listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (isPhotoSegment) {
//					Log.d("segment", "ok");
					Intent intent = new Intent(getApplicationContext(),
							PhotoActivity.class);
					intent.putExtra("data", photoGallery);
					intent.putExtra("pos", position);
					startActivity(intent);
				} else {
					Log.d("segment", "ok");
					Intent intent = new Intent(getApplicationContext(),
							VideoActivity.class);
					intent.putExtra("data",
							mAdapter.getGalleryContent(position));
					startActivity(intent);
				}
			}
		});*/
		
		if (alContent == null) getData(); 
		else showGallery();
	}
	
	private void showGallery(){
		photoGallery = new ArrayList<GalleryContent>();
		videoGallery = new ArrayList<GalleryContent>();
		for (int i = 0; i < alContent.size(); i++) {
			if (alContent.get(i).getType() == 1) { // type =1 is photo
				photoGallery.add(alContent.get(i));
			} else
				videoGallery.add(alContent.get(i));
		}
		//videoGallery.add(new GalleryContent(1, "android.resource://" + getPackageName()
		//	+ "/" + R.raw.big_buck_bunny, "Big buck bunny", "def", 2));
		mAdapter = new GalleryAdapter(this, alContent, isPhotoSegment);
		
		btnRetry.setVisibility(View.GONE);
    	prBar.setVisibility(View.GONE);
    	listView.setVisibility(View.VISIBLE);
		
		listView.setAdapter(mAdapter);
	}
	
	public void onClickLike(View v){
		
	}
	
	public void onClickComment(View v){
		CommentDialog.show(this, DataHolder.FEED_ID_SAMPLE_2);
	}

	private void getData() {
		if (NetworkUtility.checkNetworkState(getApplicationContext())) {
			btnRetry.setVisibility(View.GONE);
	    	prBar.setVisibility(View.VISIBLE);
	    	listView.setVisibility(View.GONE);
	    	int id = getIntent().getIntExtra("id", 1);
			NewRestConnector.post("", NewParamBuilder
					.BuildParams(NewParamBuilder.BuildDetailAlbumData(id)),
					new AsyncHttpResponseHandler() {
						@Override
						public void onSuccess(int arg0, String response) {
							if (response == null){
								btnRetry.setVisibility(View.VISIBLE);
						    	prBar.setVisibility(View.GONE);
						    	listView.setVisibility(View.GONE);
						    	return;
							}
							super.onSuccess(arg0, response);
							if (NewResponseTranslater.checkSuccess(response)) {
								alContent = NewResponseTranslater.getDetailGallery(response);
								showGallery(); 
							} else {
								btnRetry.setVisibility(View.VISIBLE);
						    	prBar.setVisibility(View.GONE);
						    	listView.setVisibility(View.GONE);
							}

						}

						@Override
						public void onFailure(Throwable arg0, String arg1) {
							super.onFailure(arg0, arg1);
							btnRetry.setVisibility(View.VISIBLE);
					    	prBar.setVisibility(View.GONE);
					    	listView.setVisibility(View.GONE);
						}
					});
		} else {
			btnRetry.setVisibility(View.VISIBLE);
	    	prBar.setVisibility(View.GONE);
	    	listView.setVisibility(View.GONE);
	    	
			Toast.makeText(this, "Không có kết nối",
					Toast.LENGTH_SHORT).show();
		}
	}
	
	public void onClickBack(View v){
		finish();
	}


}